namespace ScrumSprintMonitor.UI.Wpf
{
	#region #using Directives

	using System;
	using System.IO;
	using System.Windows;

	using Microsoft.SDK.Samples.VistaBridge.Library;
	using Microsoft.Win32;

	#endregion

	public class FileDialog
	{
		public static string ShowFileOpenDialog(Window parentWindow, string filePath, string title, string filter, string defaultExtension,
		                                        string defaultFileName, Guid usageIdentifier)
		{
			if (Environment.OSVersion.Version.Major >= 6)
			{
				var dlg = new CommonOpenFileDialog
				          	{
				          		Title = title,
				          		AddToMruList = true,
				          		AddExtension = true,
				          		DereferenceLinks = true,
				          		UsageIdentifier = usageIdentifier
				          	};

				string initialDirectory = Path.GetDirectoryName(filePath);
				if (Directory.Exists(initialDirectory))
				{
					dlg.InitialDirectory = initialDirectory;
				}
				dlg.Filters.Add(new CommonFileDialogFilter(filter.Split(new[] {'|'})[0], filter.Substring(filter.IndexOf('|') + 1)));

				// Show open file dialog box
				return (dlg.ShowDialog().Canceled ? null : dlg.FileName);
			}
			else
			{
				var dlg = new OpenFileDialog
				          	{
				          		Title = title,
				          		InitialDirectory = Path.GetDirectoryName(filePath),
				          		FileName = (File.Exists(filePath) ? filePath : defaultFileName),
				          		DefaultExt = defaultExtension,
				          		Filter = filter,
				          		AddExtension = true,
				          		DereferenceLinks = true
				          	};

				// Show open file dialog box
				return (dlg.ShowDialog(parentWindow) == true ? dlg.FileName : null);
			}
		}
	}
}